<template>
  <div class="bar-chart-container">
    <!-- ECharts 容器 -->
    <div
      :id="chartId"
      style="width: 100%; height: 100%"
      class="chart-content"
    ></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  name: "BarChart",
  props: {
    // 图表数据，格式：{xAxis: [], seriesData: []}
    chartData: {
      type: Object,
      required: true,
      default: () => ({
        xAxis: [],
        seriesData: [],
      }),
    },
    // 图表标题
    title: {
      type: String,
      default: "",
    },
    // y 轴名称
    yAxisName: {
      type: String,
      default: "",
    },
    chartId: { type: String, default: "Bar-chart" },
  },
  data() {
    return {
      // chartId: { type: String, default: "Line-chart" },
      myChart: null,
      backgroundData: [],
      maxValue: 0,
    };
  },
  mounted() {
    this.initChart();
  },
  watch: {
    // 监听数据变化，更新图表
    chartData(newVal) {
      this.updateChart(newVal);
    },
  },
  beforeDestroy() {
    // 销毁图表实例，释放资源
    if (this.myChart) {
      this.myChart.dispose();
      this.myChart = null;
    }
  },
  methods: {
    initChart() {
      const chartDom = document.getElementById(this.chartId);
      if (chartDom) {
        this.myChart = echarts.init(chartDom);
        this.updateChart(this.chartData);
      }
    },
    updateChart(data) {
      if (!this.myChart || !data.seriesData || data.seriesData.length === 0)
        return;
      this.maxValue = Math.max(...data.seriesData);
      // 创建背景数据（所有值都为最大值）
      this.backgroundData = new Array(data.seriesData.length).fill(
        this.maxValue
      );
      const option = {
        showBackground: true,
        // 配置背景样式
        backgroundStyle: {},
        title: {
          text: this.title,
          left: "left",
          textStyle: {
            color: "#fff", // 标题文字颜色，匹配深色背景
            fontSize: 14,
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "none",
          },
        },

        xAxis: {
          type: "category",
          data: data.xAxis,
          axisLine: {
            lineStyle: {
              color: "#ccc", // x 轴轴线颜色
            },
          },
          axisLabel: {
            color: "#fff", // x 轴文字颜色
            fontSize: "16",
          },
          axisTick: {
            show: false, // 隐藏 x 轴刻度
          },
        },
        yAxis: {
          type: "value",
          name: this.yAxisName,
          axisLine: {
            lineStyle: {
              color: "#ccc", // y 轴轴线颜色
            },
          },
          nameGap: 20, // 名称与轴线的距离
          nameTextStyle: {
            color: "#fff", // 坐标轴名称的颜色
            fontSize: "20", // 文字字体大小
            align: "right", // 文字水平对齐方式，默认自动（'left'，'center'，'right'）
            verticalAlign: "middle", // 文字垂直对齐方式，默认自动（'top'，'middle'，'bottom'
          },
          axisLabel: {
            color: "#fff", // y 轴文字颜色
            fontSize: "16",
          },
          splitLine: {
            show: false,
            // lineStyle: {
            //   color: "rgba(255,255,255,0.1)", // 网格线颜色，设为浅色透明
            // },
          },
          axisTick: {
            show: false, // 隐藏 y 轴刻度
          },
        },
        series: [
          {
            type: "pictorialBar",
            symbolRepeat: "fixed",
            symbolMargin: 2, //图形的两边间隔
            symbolSize: [10, 10],
            symbol: "rect",
            barWidth: "10%", // 比数据柱稍宽
            itemStyle: {
              color: "rgba(255, 255, 255, 0.15)", // 半透明灰色
              borderRadius: 6, // 圆角
            },
            tooltip: {
              trigger: "axis",
              show: false,
            },
            z: 0, // 层级最低，显示在最底层
            data: this.backgroundData,
          },
          {
            type: "pictorialBar",
            symbolRepeat: "fixed",
            symbolMargin: 2, //图形的两边间隔
            symbol: "rect",
            symbolClip: true, //是否裁剪图形
            symbolSize: [10, 10],
            name: this.title,
            data: data.seriesData,
            barWidth: "10%", // 调整柱子宽度
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: "#6495ED" }, // 柱子顶部颜色
                { offset: 1, color: "#6495EB" }, // 柱子底部颜色，模拟渐变
              ]),
              borderRadius: [6, 6, 0, 0], // 柱子顶部圆角
            },
            label: {
              show: false, // 可根据需求显示柱子上的数值
              position: "top",
              color: "#fff",
            },
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "#6495Ee" },
                  { offset: 1, color: "#6495EC" },
                ]),
              },
            },
          },
        ],
        color: ["#fcd080"],
        grid: {
          // left: "2%",
          // top: "10%",
          // right: "2%",
          // bottom: "5%",
          // containLabel: true,
          left: "0",
          top: "10%",
          right: "0",
          bottom: "0",
          containLabel: true,
        },
      };
      this.myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
.bar-chart-container {
  /* width: 100%;
  height: 300px; 可根据需求调整高度 */
  /* width: 100%;
  height: 100%; */
  /* box-sizing: border-box; */
  position: relative;
  width: 100%;
  height: 100%;
  /* background-color: #1f262d; */
  border-radius: 8px;
  /* box-shadow: 0 4px 12px rgba(0, 0, 0, 1); */
  /* overflow: hidden; */
}
.chart-content {
  width: 100%;
  height: 100%;
}
</style>